package ws

import (
	"bytes"
	"errors"

	"golang.org/x/net/websocket"

	"gitee.com/hasika/common-server/conn/inet"
)

type FrameReader struct {
}

func (w *FrameReader) NonBlockSlicePacketBytes(conn inet.PublicConn) ([][]byte, error) {
	return nil, errors.New("not implement")
}

func (w *FrameReader) BlockSlicePacketBytes(conn inet.PublicConn) ([]byte, error) {
	ws := conn.(*Connection)
	buffer := make([]byte, 4096)
	err := websocket.Message.Receive(ws.Conn, &buffer)
	if err != nil {
		return nil, err
	}
	return buffer, nil
}

func (w *FrameReader) WriteOnePacketBytes(conn inet.PublicConn, data []byte) error {
	return conn.GetWriteCtx().DoWrite(conn, bytes.NewBuffer(data))
}

func (w *FrameReader) Return(data *bytes.Buffer) {
	return
}
